<?php
/*
Copyright 2013 CFour Dev.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

include_once "lib/classes.php";
include_once "config.php";
session_start();
$mysql = new MySQL($config["host"], $config["user"], $config["password"], $config["db"], $config["prefix"]);
if(isset($_REQUEST['action']))
{
	switch($_REQUEST['action'])
	{
		case "login":
		{  
			$array = $mysql->AuthUser($_REQUEST['user'], $_REQUEST['password']);
			if($array == 0) die('Auth fail');
			else 
			{
				if(!isset($_REQUEST['remember']))
				{
					$_SESSION['id'] = $array['user_id'];
					$_SESSION['password'] = $array['user_password'];
				}
				elseif($_REQUEST['remember'] == 1)
				{
					setcookie("id", $array['user_id']);
					setcookie("password", $array['user_password']);
				}
				else
				{
					$_SESSION['id'] = $array['user_id'];
					$_SESSION['password'] = $array['user_password'];
				}
				die('<META HTTP-EQUIV="REFRESH" CONTENT="0;URL=index.php">');
			}
			break;		
		}
		case "logout":
		{
			if(isset($_COOKIE['id']) && isset($_COOKIE['password']))
			{
				setcookie("id", null);
				setcookie("password", null);
				die('<META HTTP-EQUIV="REFRESH" CONTENT="0;URL=login.php">');
			}
			else if(isset($_SESSION['id']) && isset($_SESSION['password']))
			{
				session_destroy();
				die('<META HTTP-EQUIV="REFRESH" CONTENT="0;URL=login.php">');
			}
			break;
		}
		default : 
		{

		}
	}
}
//This function was copied from phpBB, thx to phpBB team.
function generate_pagination($base_url, $num_items, $per_page, $start_item, $add_prevnext_text = false, $tpl_prefix = "", $start="start")
{
	$per_page = ($per_page <= 0) ? 1 : $per_page;
	$seperator = " ";
	$total_pages = ceil($num_items / $per_page);
	if ($total_pages == 1 || !$num_items) return "";
	$on_page = floor($start_item / $per_page) + 1;
	$url_delim = (strpos($base_url, "?") === false) ? "?" : ((strpos($base_url, '?') === strlen($base_url) - 1) ? "" : "&amp;");
	$page_string = ($on_page == 1) ? "<strong>1</strong>" : "<a href=\"{$base_url}\">1</a>";
	if ($total_pages > 5)
	{
		$start_cnt = min(max(1, $on_page - 4), $total_pages - 5);
		$end_cnt = max(min($total_pages, $on_page + 4), 6);
		$page_string .= ($start_cnt > 1) ? " ... " : $seperator;
		for ($i = $start_cnt + 1; $i < $end_cnt; $i++)
		{
			$page_string .= ($i == $on_page) ? "<strong>{$i}</strong>" : "<a href=\"{$base_url}{$url_delim}{$start}=" . (($i - 1) * $per_page) . "\">{$i}</a>";
			if ($i < $end_cnt - 1) $page_string .= $seperator;
		}
		$page_string .= ($end_cnt < $total_pages) ? " ... " : $seperator;
	}
	else
	{
		$page_string .= $seperator;
		for ($i = 2; $i < $total_pages; $i++)
		{
			$page_string .= ($i == $on_page) ? "<strong>{$i}</strong>" : "<a href=\"{$base_url}{$url_delim}{$start}=" . (($i - 1) * $per_page) . "\">{$i}</a>";
			if ($i < $total_pages) $page_string .= $seperator;
		}
	}
	$page_string .= ($on_page == $total_pages) ? "<strong>{$total_pages}</strong>" : "<a href=\"{$base_url}{$url_delim}{$start}=" . (($total_pages - 1) * $per_page) . "\">{$total_pages}</a>";
	if ($add_prevnext_text)
	{
		if ($on_page != 1) $page_string = "<a href=\"{$base_url}{$url_delim}{$start}=" . (($on_page - 2) * $per_page) . "\">h</a>{$page_string}";
		if ($on_page != $total_pages) $page_string .= "<a href=\"{$base_url}{$url_delim}{$start}=" . ($on_page * $per_page) . "\">j</a>";
	}
	return $page_string;
}
?>